# 导入flask库
from flask import Flask, render_template
from markupsafe import escape

app = Flask(__name__)  # 注册运行对象


@app.route('/')
@app.route('/index')
@app.route('/home')  # 装饰器为函数绑定对应的 URL：http://127.0.0.1:5000/home
def index():  # 视图函数（view function）：本质上是获取url地址发送过来的请求
    name = '张蓝'  # str
    movies = [
        {'title': 'My Neighbor Totoro', 'year': '1988'},
        {'title': 'Dead Poets Society', 'year': '1989'},
        {'title': 'A Perfect World', 'year': '1993'},
        {'title': 'Leon', 'year': '1994'},
        {'title': 'Mahjong', 'year': '1996'},
        {'title': 'Swallowtail Butterfly', 'year': '1996'},
        {'title': 'King of Comedy', 'year': '1999'},
        {'title': 'Devils on the Doorstep', 'year': '1999'},
        {'title': 'WALL-E', 'year': '2008'},
        {'title': 'The Pork of Music', 'year': '2012'},
    ]
    return render_template('test.html', name=name, movies=movies)


@app.route('/about')
def hello_about():  # put application's code here
    return 'Welcome to About Page!'


@app.route('/test/<username>')
# 使用url地址传递动态参数 username，函数需要接受这个参数，不然调用不了
def test(username):
    return f'User: {escape(username)}'


if __name__ == '__main__':
    app.run()
